package cn.com.zero.common.message.service.impl;

import cn.com.zero.base.service.BaseService;
import cn.com.zero.common.message.cache.MessageCacheRedis;
import cn.com.zero.common.message.service.IMessageService;
import cn.com.zero.common.message.vo.MessageVO;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 消息service实现类
 * @createTime 2023/3/1 14:08
 */
@Service
public class MessageService extends BaseService implements IMessageService {
    private MessageCacheRedis messageCache;

    @Override
    public String queryMessage(String phone) {
        List<MessageVO> messageList = messageCache.getMessageList(phone);
        if (CollectionUtils.isEmpty(messageList)) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        // 不加pre标签，会导致浏览器访问时，换行不生效
        stringBuilder.append("<pre>");
        for (MessageVO messageVO : messageList) {
            stringBuilder.append(messageVO.getPhone())
                    .append("\t")
                    .append(DateUtil.formatDateTime(messageVO.getSendTime()))
                    .append("\t")
                    .append(messageVO.getMessage())
                    .append("\n")
                    .append("\n")
            ;
        }
        stringBuilder.append("</pre>");
        return stringBuilder.toString();
    }

    @Autowired
    public void setMessageCache(MessageCacheRedis messageCache) {
        this.messageCache = messageCache;
    }
}
